package com.heima.controller;

import com.heima.pojo.dto.PageDto;
import com.heima.pojo.dto.PolicyDto;
import com.heima.pojo.dto.PolicyPageDto;
import com.heima.pojo.dto.VmPolicyPageDto;
import com.heima.pojo.vo.PolicyPageVo;
import com.heima.pojo.vo.VmPolicyVo;
import com.heima.pojo.vo.VmUsePolicyPageVo;
import com.heima.service.PolicyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/policy")
@Slf4j
public class PolicyController {

    @Autowired
    private PolicyService policyService;


    /**
     * 策略搜索
     */
    @GetMapping("/search")
    public PolicyPageVo pageQuery(PolicyPageDto dto){
        log.info("dto:{}",dto);
        return policyService.pageQuery(dto);
    }
    /**
     * 删除策略
     */
    @DeleteMapping("/{policyId}")
    public Boolean delById(@PathVariable Integer policyId){
        log.info("id:{}",policyId);
        policyService.delById(policyId);
        return true;
    }

    /**
     * 取消策略
     */
    @PutMapping("/{policyId}/{innerCode}")
    public Boolean cancelPolicy(@PathVariable Integer policyId,@PathVariable String innerCode){
        log.info("id:{},innerCode:{}",policyId,innerCode);
        policyService.save(policyId,innerCode);
        return true;
    }

    /**
     * 新增策略
     */
    @PostMapping
    public Boolean addPolicy(@RequestBody PolicyDto dto){
        log.info("dto:{}",dto);
        policyService.add(dto);
        return true;
    }

    /**
     * 查询售货机策略
     */
    @GetMapping("/{innerCode}")
    public VmPolicyVo queryVmPolicy(@PathVariable String innerCode){
        log.info("InnerCode:{}",innerCode);
        VmPolicyVo vmPolicyVo = null;
        try {
            vmPolicyVo = policyService.queryVmPolicy(innerCode);
        } catch (Exception e) {
            log.info("该售货机没有优惠",e);
        }
        return vmPolicyVo;
    }

    /**
     * 根据策略搜索售货机
     */
    @GetMapping("/vmList/{policyId}")
    public VmUsePolicyPageVo pageQueryByPolicyId(@PathVariable Integer policyId, VmPolicyPageDto dto){
        log.info("policyId:{},dto:{}",policyId,dto);
        dto.setPolicyId(policyId);
        return policyService.pageQueryByPolicyId(dto);
    }

    /**
     * 修改策略
     */
    @PutMapping("/{policyId}")
    public Boolean update(@PathVariable Integer policyId, PolicyDto dto){
        log.info("policyId:{},dto:{}",policyId,dto);
        dto.setPolicyId(policyId);
        policyService.update(dto);
        return true;
    }
}
